#!/usr/bin/perl -w
require "ctime.pl";
                                                                                                                   
# ALL PURPOSE GENERAL PROGRAM
my $debug = 0;


sub getTime {
  my @times = split(' ', &ctime(time));
  my $time = $times[4].' '.$times[1].' '.$times[2]." ".$times[3];
  #print STDERR "\*\*",$times[3],"\n";
  return $time;
};
 
sub subtTime {
  my ($time1,$time2) = @_;
#time2 > time1
  $time1 =~ s/:/ /g; $time2 =~ s/:/ /g;
  my @times1 = split(' ',$time1);
  my @times2 = split(' ',$time2);
  $times1[1] = mon2num($times1[1]);
  $times2[1] = mon2num($times2[1]);
  my ($year,$month,$day,$hour,$min,$sec);
  #sec
    $sec = $times2[5] - $times1[5];
    if($sec < 0) { $times2[4]--; $sec+=60; }
  #min
    $min = $times2[4] - $times1[4];
    if($min < 0) { $times2[3]--; $min+=60; }
  #hour
    $hour = $times2[3] - $times1[3];
    if($hour < 0) { $times2[2]--; $hour+=24; }
  #days - grossly inaccurate
    $day = $times2[2] - $times1[2];
    if($day < 0) { $times2[1]--; $day+=30; }
  #months
    $month = $times2[1] - $times1[1];
    if($month < 0) { $times2[0]--; $month+=12; }
  #year
    $month = $times2[0] - $times1[0];
  if($debug > 1) { print STDERR "$year,$month,$day,$hour,$min,$sec"; }
  if($year) {
    return "$year year(s), $month month(s)";
  } elsif($month) {
    return "$month month(s), $day day(s)";
  } elsif($day) {
    return "$day day(s), $hour hour";
  } elsif($hour) {
    return "$hour hour, $min min";
  } elsif($min) {
    return "$min min, $sec sec";
  } elsif($sec) {
    return "$sec sec";
  }
  return 0;
};
 
sub mon2num {
  my ($mon) = @_;
  my @mons = ("Jan","Feb","Mar","Apr","May","Jun",
              "Jul","Aug","Sep","Oct","Nov","Dec",);
  for(my $i=0; $i<=$#mons; $i++) {
    if($mon eq $mons[$i]) {
      return $i+1;
    }
  }
  return 0;
};

return 1;
